package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.School;
import com.cfets.annualAssessment.mapper.SchoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/1/24.
 */
@Service
public class SchoolService {
    @Autowired
    private SchoolMapper schoolMapper;

    public List<School> getSchoolPage(School school) {
        Page<?> page = PageHelper.startPage(school.getPage(), school.getRows());
        List<School> schools=null;
        Example example=new Example(School.class);
//        example.setOrderByClause("school_name DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(school.getSchool_name())){
            criteria.andLike("school_name","%"+school.getSchool_name()+"%");
        }
        schools = schoolMapper.selectByExample(example);
        school.setTotalCount(page.getTotal());
        return schools;
    }

    public boolean newSchool(School school) {
        return schoolMapper.insertSelective(school)>0;
    }

//    public boolean updateSchool(School school) {
//        return schoolMapper.updateByPrimaryKey(school)>0;
//    }

    public boolean deleteSchoolById(int id) {
        return schoolMapper.deleteByPrimaryKey(id)>0;
    }

    public boolean deleteAllSchool() {
        Example example=new Example(School.class);
        return schoolMapper.deleteByExample(example)>0;
    }

    public List<School> getSchool(){
        return schoolMapper.selectAll();
    }
}
